package plp.funcoo.dec.classes;

import plp.funcoo.dec.func.FuncDefinition;
import plp.funcoo.dec.parameter.ParameterDeclaration;
import plp.funcoo.dec.parameter.ParameterList;
import plp.funcoo.environment.compilation.CompilationContext;
import plp.funcoo.environment.compilation.CompilationEnvironment;
import plp.funcoo.exceptions.ClassAlreadyDeclared;
import plp.funcoo.exceptions.ClassNotDeclared;
import plp.funcoo.exceptions.MethodAlreadDeclared;
import plp.funcoo.exceptions.MethodNotDeclared;
import plp.funcoo.exceptions.ParameterNumberIncorrect;
import plp.funcoo.exceptions.ParameterTypeIncorrect;
import plp.funcoo.exceptions.VarAlreadyDeclared;
import plp.funcoo.exceptions.VarNotDeclared;
import plp.funcoo.expressions.Expression;
import plp.funcoo.expressions.Identifier;

public abstract class SignatureDec implements Declaration {

	
	protected Identifier name;
	
	protected ParameterList parameters;
	
	protected Expression expression;
	
	public SignatureDec(Identifier name,ParameterList parameters ,Expression expression)
	{
		this.name = name;
		this.expression = expression;
		this.parameters = parameters;	
	}
	public boolean typeCheck(CompilationEnvironment envi) throws VarAlreadyDeclared, VarNotDeclared, ClassAlreadyDeclared, ClassNotDeclared, MethodAlreadDeclared, MethodNotDeclared, ParameterTypeIncorrect, ParameterNumberIncorrect
	{
		ParameterList parameters = this.parameters.makeCopy();
		if(parameters.typeCheck(envi))
		{
			envi.increase();
			ParameterDeclaration auxPar;
			while(parameters.length()>0)
			{
				auxPar = parameters.getNext();
				envi.map(auxPar.getId(),auxPar.getType());
			}
			
			boolean aux;
			envi.mapParametersMethod(this.name,new FuncDefinition(this.parameters.makeCopy(),this.expression.makeCopy()));
			((CompilationContext)envi).setAnalysingMethod(this.name);
			aux = this.expression.typeCheck(envi);
			((CompilationContext)envi).setAnalysingMethod(null);
			envi.restore();
			return  aux;
		}
		else 
			return false;
	}
	
}
